package com.ucas.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("App")
public class DownLoadController {
	@RequestMapping("/downLoad.do")
	public void downLoadFile(HttpServletRequest request,HttpServletResponse response) {
		OutputStream outputStream=null;
		InputStream inputStream=null;
		try {
			File file = new File("K://GooleDownload/spring-tool-suite-3.9.1.RELEASE-e4.7.1a-win32-x86_64.zip");
			String fileName = file.getName();
			response.setContentType("application/zip");
			response.setHeader("Location",fileName);
			//fileName应该是编码后的(utf-8) 
			response.setHeader("Content-Disposition", "attachment; filename=" + fileName); 
			response.setContentLength((int) file.length());
			outputStream = response.getOutputStream();
			inputStream = new FileInputStream(file.getPath());
			byte[] buffer = new byte[1024];
			int i = -1;
			while ((i = inputStream.read(buffer)) != -1) {
			   outputStream.write(buffer, 0, i);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(outputStream !=null) {
				try {
					outputStream.flush();
					outputStream.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(inputStream !=null) {
				try {
					inputStream.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
